package com.ytjj.qmyx.supplychain.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.model.request.CebOrderRequest;
import com.ytjj.qmyx.supplychain.common.model.response.CebOrderResponse;
import com.ytjj.qmyx.supplychain.mapper.CebOrderGoodsinfoMapper;
import com.ytjj.qmyx.supplychain.mapper.CebOrderMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.CebOrder;
import com.ytjj.qmyx.supplychain.mapper.entity.CebOrderGoodsinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CebOrderService {
    @Resource
    private CebOrderMapper cebOrderMapper;

    @Resource
    private CebOrderGoodsinfoMapper cebOrderGoodsinfoMapper;

    public CommonPage getCebOrderList(CebOrderRequest cebOrderRequest){
        PageHelper.startPage(cebOrderRequest.getPageNum(), cebOrderRequest.getPageSize());
        List<CebOrderResponse> cebOrderResponseList=cebOrderMapper.queryCebOrderList(cebOrderRequest);
        cebOrderResponseList.stream().forEach(item -> {
            item.setCebOrderGoodsList(cebOrderMapper.queryCebOrderGoodsinfoList(item.getId()));
        });
        return CommonPage.restPage(cebOrderResponseList);
    }

    public CebOrderResponse findByOrderNo(String orderNo){
        CebOrderResponse cebOrderResponse = cebOrderMapper.findByOrderNo(orderNo);
        if(null != cebOrderResponse) {
            cebOrderResponse.setCebOrderGoodsList(cebOrderMapper.queryCebOrderGoodsinfoList(cebOrderResponse.getId()));
            return cebOrderResponse;
        }else{
            return null;
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public CommonResult saveCebOrder(CebOrder cebOrder, List<CebOrderGoodsinfo> cebOrderGoodsinfoList){
        if(null==cebOrder || null==cebOrderGoodsinfoList){
            return CommonResult.failed("保存失败！");
        }
        CebOrderResponse oldCebOrder=cebOrderMapper.findByOrderNo(cebOrder.getOrderNo());
        if(null != oldCebOrder){
            return CommonResult.failed("已存在此订单！");
        }
        int saveResult=cebOrderMapper.insert(cebOrder);
        if(saveResult>0){
            cebOrderGoodsinfoList.stream().forEach(item -> {
                item.setOrderId(cebOrder.getId());
                cebOrderGoodsinfoMapper.insert(item);
            });
            return CommonResult.success();
        }else{
            return CommonResult.failed("保存失败！");
        }
    }

    public CommonResult updateCebOrderReturn(String orderNo,String returnStatus,String returnTime,String returnInfo){
        int result=cebOrderMapper.updateCebOrderReturn(orderNo,returnStatus,returnTime,returnInfo);
        if(result>0){
            return CommonResult.success();
        }else{
            return CommonResult.failed("更新失败！");
        }
    }

    public CommonResult updateSendStatus(String orderNo,Integer sendStatus, Date sendTime){
        int result=cebOrderMapper.updateSendStatus(orderNo,sendStatus,sendTime);
        if(result>0){
            return CommonResult.success();
        }else{
            return CommonResult.failed("更新失败！");
        }
    }
}
